package be.jprojects.subscription.gui;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;

import javax.swing.JTextField;
import javax.swing.text.Document;

public class SpotlightField extends JTextField {
    private Color from;
    private Color to;
    private Insets insets;
    private Point view = new Point();
	
	public SpotlightField() {
		super();
		init();
	}

	public SpotlightField(Document doc, String text, int columns) {
		super(doc, text, columns);
		init();
	}

	public SpotlightField(int columns) {
		super(columns);
		init();
	}

	public SpotlightField(String text, int columns) {
		super(text, columns);
		init();
	}

	public SpotlightField(String text) {
		super(text);
		init();
	}
	
	private void init() {
        this.from = Color.blue;
        this.to = Color.green;
        insets = getInsets();
	}
	
	public void paintComponent(Graphics g) {
        setForeground( getBackground() );
        super.paintComponent(g);
 
        //  Get the text to paint, assuming left justification
 
        getInsets( insets );
        view.x = insets.left;
        view.y = insets.top;
        int offset = viewToModel( view );
        String text = getText().substring(offset);
 
        //  Create the GradientPaint object
 
        FontMetrics fm = g.getFontMetrics();
        int width = fm.stringWidth( text );
        int x = insets.left;
        int y = fm.getAscent() + insets.top;
        GradientPaint paint = new GradientPaint(x, 0, from, x + width, 0, to);
 
        //  Use the GradientPaint object to overwrite the existing text
 
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(paint);
        g2d.drawString(text, x, y);
	}
	
	

}
